package com.wc.算法提高课.A第一章_动态规划.状态压缩DP.炮兵阵地;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/11 13:02
 * @description https://www.acwing.com/problem/content/294/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110, M = 1 << 11;
    static int[] g = new int[N];
    // f[i][j][k] 表示第i行状态为 j,第i - 1行状态为k的最大炮兵数量
    static int[][][] f = new int[2][M][M];
    static int[] state = new int[M];
    static int[] cnt = new int[M];
    static char[] s;
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            s = sc.next().toCharArray();
            for (int j = 0; j < m; j++) {
                if (s[j] == 'H') g[i] += 1 << j;
            }
        }

        for (int i = 0; i < 1 << m; i++) {
            if (check(i)) {
                state[++state[0]] = i;
                cnt[i] = count(i);
            }

        }
        for (int i = 1; i <= n + 2; i++) {
            for (int j = 1; j <= state[0]; j++) {
                for (int k = 1; k <= state[0]; k++) {
                    for (int u = 1; u <= state[0]; u++) {
                        // a 在 i, b 在 i - 1, c在i - 2;
                        int a = state[j], b = state[k], c = state[u];
                        // 首先他们三个不能冲突
                        if ((a & b | (b & c) | (c & a)) > 0) continue;
                        if (((g[i] & a) | (g[i - 1] & b)) > 0) continue;
                        f[i & 1][a][b] = Math.max(f[i & 1][a][b], f[i - 1 & 1][b][c] + cnt[a]);
                    }
                }
            }
        }
        out.println(f[n + 2 & 1][0][0]);
        out.flush();
    }

    static boolean check(int state) {
        for (int i = 0; i < m; i++) {
            if ((state >> i & 1) == 1 && ((state >> i + 1 & 1) | (state >> i + 2 & 1)) == 1)
                return false;
        }
        return true;
    }

    static int count(int state) {
        int res = 0;
        for (int i = 0; i < m; i++) {
            res += state >> i & 1;
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
